package com.kun.crawling.controller;

import com.kun.crawling.config.DataCrawlingProperties;
import com.kun.crawling.service.DataCrawlingService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author kun.li
 */
@RestController
@RequiredArgsConstructor
@Slf4j
public class DataCrawlingController {

    @Autowired
    private List<DataCrawlingService> dataCrawlingServiceList;

    private final DataCrawlingProperties dataCrawlingProperties;


    @GetMapping("dataCrawling")
    public void dataCrawling() {
        try {
            String type = dataCrawlingProperties.getType();
            for (DataCrawlingService dataCrawlingService : dataCrawlingServiceList) {
                if (dataCrawlingService.support(type)) {
                    dataCrawlingService.dataCrawling();
                    return;
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        }
    }
}
